package com.jxl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jxl.entity.dto.ScheduleDTO;
import com.jxl.entity.po.Schedule;
import com.jxl.entity.query.SchedulePageQuery;
import com.jxl.entity.vo.PageResponse;
import com.jxl.entity.vo.ScheduleVO;
import com.jxl.entity.vo.UserVO;
import com.jxl.mapper.ScheduleMapper;
import com.jxl.service.IScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 排班表 服务实现类
 * </p>
 *
 * @author 金小梁
 * @since 2025-06-09
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements IScheduleService {
    @Autowired
    private ScheduleMapper scheduleMapper;
    /**
     * 分页查询
     *
     * @param schedulePageQuery
     * @return
     */
    @Override
    public PageResponse<ScheduleVO> querySchedules(SchedulePageQuery schedulePageQuery) {
        PageHelper.startPage(schedulePageQuery.getPageNum(),  schedulePageQuery.getPageSize());
        Page<ScheduleVO> scheduleVOPage = scheduleMapper.selectByPageQuery(schedulePageQuery);
        PageResponse<ScheduleVO> pageResponse = PageResponse.of(scheduleVOPage,  ScheduleVO.class);
        return pageResponse;
    }

    /**
     * 新增
     *
     * @param scheduleDTO
     */
    @Override
    public void addSchedule(ScheduleDTO scheduleDTO) {
        //查询是否有编号相同的线路
        List<Schedule> list = query().eq("schedule_no", scheduleDTO.getScheduleNo()).list();
        if(list.size()>0){
            throw new RuntimeException("该排班编号已存在");
        }
        Schedule schedule = new Schedule();
        BeanUtil.copyProperties(scheduleDTO, schedule);
        //保存
        save(schedule);
    }

    /**
     * 修改
     *
     * @param scheduleDTO
     */
    @Override
    public void updateSchedule(ScheduleDTO scheduleDTO) {
        //查询是否有编号相同的线路，排除当前修改的线路
        List<Schedule> list = query().eq("schedule_no", scheduleDTO.getScheduleNo()).ne("id", scheduleDTO.getId()).list();
        if(list.size()>0){
            throw new RuntimeException("该排班编号已存在");
        }
        Schedule schedule = new Schedule();
        BeanUtil.copyProperties(scheduleDTO, schedule);
        updateById(schedule);
    }
}
